package modules.cms.models
{
	import flash.utils.getQualifiedClassName;
	import devdoo.fx.UseDB;
	import devdoo.fx.UseUtil;
	import devdoo.fx.UseString;
	import devdoo.fx.interfaces.ITableModel;
	
	public class UseCmsContent implements ITableModel
	{
		private var _classNameModel:String = 'UseCmsContent';
		private var _table:String = 'menu_content';
		private var _alias:String = 'USE_MENU_CONTENT';
		private var _dataAliasUpdate:Object = new Object();
		private var _dataUpdate:Object = new Object();
		private var _fields:Object = new Object();
		private var _idField:String = 'id';
		private var _idFieldAlias:String = 'USE_MENU_CONTENT_id';
		private var _useTableAlias:Boolean;
		private var _isSave:Boolean = false;
		
		public function UseCmsContent(){
			this._fields.USE_MENU_CONTENT_changed_in = "changed_in";
			this._fields.USE_MENU_CONTENT_created_in = "created_in";
			this._fields.USE_MENU_CONTENT_finish_in = "finish_in";
			this._fields.USE_MENU_CONTENT_id = "id";
			this._fields.USE_MENU_CONTENT_is_active = "is_active";
			this._fields.USE_MENU_CONTENT_is_editable = "is_editable";
			this._fields.USE_MENU_CONTENT_last_changed_id = "last_changed_id";
			this._fields.USE_MENU_CONTENT_layout = "layout";
			this._fields.USE_MENU_CONTENT_start_in = "start_in";
			this._fields.USE_MENU_CONTENT_use_contract_id = "use_contract_id";
			this._fields.USE_MENU_CONTENT_use_register_id = "use_register_id";
			this._fields.USE_MENU_CONTENT_view_total = "view_total";
		}
		
		public function get tableAlias():Object{
			var table:Object = new Object();
			table[this._alias] = this._table;
			return table;
		}
		
		public function get classNameModel():String{
			return this._classNameModel;
		}
		
		public function get table():String{
			return this._table;
		}
		
		public function get idField():String{
			return this._idField;
		}
		
		public function get idFieldAlias():String{
			return this._idFieldAlias;
		}
		
		public function get alias():String{ return this._alias; }
		
		public function fields(fieldsList:Array, useTableAlias:Boolean=false):Object{
			var data:Object = new Object();
			var fields:Object = this._fields;
			var errorFields:String = '';
			var error:Boolean = true;
			var item:String;
			var field:String;
			var i:String;
			var j:String;
			
			this._useTableAlias = useTableAlias;
			if(typeof(fieldsList[0]) == 'string' && fieldsList[0] == '*' ){
				if(useTableAlias){
					for ( field in this._fields){
						data[field] = this._fields[field];
					}
				}
				else{
					for ( field in this._fields){
						data[(this._fields[field])] = this._fields[field];
					}
				}
				for (item in this._setVar){
					data[item] = this._setVar[item];
				}
				for (item in this._expr){
					data[item] = this._expr[item];
				}
				return data;
			}
			for (j in fieldsList){
				if(useTableAlias){
					for (i in this._fields){
						if( fields[i] == fieldsList[j] ){
							data[this._alias+'_'+fields[i]] = fieldsList[j];
							error = false;
							continue;
						}
					}
				}
				else{
					for (i in fields){
						if(typeof(fieldsList[j])== 'object'){
							for (var k:String in fieldsList[j]) {
								data[k] = fieldsList[j][k];
								error = false;
								continue;
							}
						}
						else if( fields[i] == fieldsList[j] ){
							data[(fields[i])] = fieldsList[j];
							error = false;
							continue;
						}
					}
				}
				if(error) errorFields += fieldsList[j]+' :: ';
				error = true;
			}
			if(errorFields != ''){
				throw new Error(getQualifiedClassName(this) +'::Error( :: '+errorFields+')', 'Error :: Campos não encontrado no modelo da tabela '+this._table);
			}
			for (item in this._setVar){
				data[item] = this._setVar[item];
			}
			for (item in this._expr){
				data[item] = this._expr[item];
			}
			return data;
		}
		
		private var _expr:Object = new Object();
		public function expr(field:String, expr:String, alias:String = null, useTableAlias:Boolean=false):UseCmsContent{
			var fields:Object = this._fields;
			var error:Boolean = true;
			for (var i:String in fields){
				if( fields[i] == field ) error = false;
			}
			if(error){
				throw new Error(getQualifiedClassName(this) +'::Error exp('+field+')', 'Error :: Campo utilizado em exp(?) não encontrado no modelo da tabela '+this._table);
				return null;
			}
			if(alias == null){
				if(useTableAlias)
					alias = this._alias + '_' + field;
				else
					alias = field;
			}
			else{
				if(useTableAlias)
					alias =  this._alias + '_' + alias;
			}
			this._expr[(alias)] = UseString.replace(expr, '?', this._alias + '.' + field) +' as ' + alias;
			return this;
		}
		
		private var _setVar:Object = new Object();
		public function setVar(field:String, value:String):UseCmsContent{
			this._setVar[field] = value;
			return this;
		}
		public function get dataAlias():Object{
			return _dataAliasUpdate;
		}
		
		public function set data(data:Object):void{
			var len:int = this._alias.length+1;
			var alias:String;
			var tempIndex:String;
			for (var index:* in data){
				alias = index.substring(0,len);
				if(alias == this._alias+'_'){
					tempIndex = index.replace(alias,'');
				}
				if(this.hasOwnProperty(tempIndex)){
					this[tempIndex] = data[index];
				}
			}
		}
		
		public function get data():Object{
			return _dataUpdate;
		}
		
		public function find(id:int, callBack:Function):void{
			var useDB:UseDB = new UseDB('SELECT');
			useDB.setFetchMode(UseDB.FETCH_OBJ)
				.from(tableAlias)
				.fields(['*'])
				.where(this._alias +'.'+ this.idField + ' = ?', String(id))
				.fetch('All')
				.connect(callBack);
		}
		
		public function remove(id:int, callBack:Function):void{
			var useDB:UseDB = new UseDB('DELETE');
			useDB.setFetchMode(UseDB.FETCH_OBJ)
			useDB.table(table)
				.where(this._alias +'.'+ this.idField + ' = ?', String(id))
				.connect(callBack);
		}
		
		public function update(callBack:Function):void{
			var useDB:UseDB = new UseDB('UPDATE');
			useDB.table(table)
				.data(this._dataUpdate)
				.where(this.idField + ' = ?', this.id)
				.connect(callBack);
		}
		
		public function fetch(callBack:Function, type:String = 'All'):void{
			var useDB:UseDB = new UseDB('SELECT');
			useDB.from(tableAlias)
				.fields(['*'])
				.fetch(type)
				.connect(callBack);
		}
		
		private var _changed_in:*;
		public function set changed_in(changed_in:*):void{
			this._changed_in = UseUtil.checkSetData('changed_in', changed_in);
			this._dataAliasUpdate['USE_MENU_CONTENT_changed_in'] = this._dataUpdate['changed_in'] = changed_in;
			this._isSave = true;
		}
		public function get changed_in():*{ return this._changed_in; }
		
		private var _created_in:*;
		public function set created_in(created_in:*):void{
			this._created_in = UseUtil.checkSetData('created_in', created_in);
			this._dataAliasUpdate['USE_MENU_CONTENT_created_in'] = this._dataUpdate['created_in'] = created_in;
			this._isSave = true;
		}
		public function get created_in():*{ return this._created_in; }
		
		private var _finish_in:*;
		public function set finish_in(finish_in:*):void{
			this._finish_in = UseUtil.checkSetData('finish_in', finish_in);
			this._dataAliasUpdate['USE_MENU_CONTENT_finish_in'] = this._dataUpdate['finish_in'] = finish_in;
			this._isSave = true;
		}
		public function get finish_in():*{ return this._finish_in; }
		
		private var _id:*;
		public function set id(id:*):void{
			this._id = UseUtil.checkSetData('id', id);
			this._dataAliasUpdate['USE_MENU_CONTENT_id'] = this._dataUpdate['id'] = id;
			this._isSave = true;
		}
		public function get id():*{ return this._id; }
		
		private var _is_active:*;
		public function set is_active(is_active:*):void{
			this._is_active = UseUtil.checkSetData('is_active', is_active);
			this._dataAliasUpdate['USE_MENU_CONTENT_is_active'] = this._dataUpdate['is_active'] = is_active;
			this._isSave = true;
		}
		public function get is_active():*{ return this._is_active; }
		
		private var _is_editable:*;
		public function set is_editable(is_editable:*):void{
			this._is_editable = UseUtil.checkSetData('is_editable', is_editable);
			this._dataAliasUpdate['USE_MENU_CONTENT_is_editable'] = this._dataUpdate['is_editable'] = is_editable;
			this._isSave = true;
		}
		public function get is_editable():*{ return this._is_editable; }
		
		private var _last_changed_id:*;
		public function set last_changed_id(last_changed_id:*):void{
			this._last_changed_id = UseUtil.checkSetData('last_changed_id', last_changed_id);
			this._dataAliasUpdate['USE_MENU_CONTENT_last_changed_id'] = this._dataUpdate['last_changed_id'] = last_changed_id;
			this._isSave = true;
		}
		public function get last_changed_id():*{ return this._last_changed_id; }
		
		private var _layout:*;
		public function set layout(layout:*):void{
			this._layout = UseUtil.checkSetData('layout', layout);
			this._dataAliasUpdate['USE_MENU_CONTENT_layout'] = this._dataUpdate['layout'] = layout;
			this._isSave = true;
		}
		public function get layout():*{ return this._layout; }
		
		private var _start_in:*;
		public function set start_in(start_in:*):void{
			this._start_in = UseUtil.checkSetData('start_in', start_in);
			this._dataAliasUpdate['USE_MENU_CONTENT_start_in'] = this._dataUpdate['start_in'] = start_in;
			this._isSave = true;
		}
		public function get start_in():*{ return this._start_in; }
		
		private var _use_contract_id:*;
		public function set use_contract_id(use_contract_id:*):void{
			this._use_contract_id = UseUtil.checkSetData('use_contract_id', use_contract_id);
			this._dataAliasUpdate['USE_MENU_CONTENT_use_contract_id'] = this._dataUpdate['use_contract_id'] = use_contract_id;
			this._isSave = true;
		}
		public function get use_contract_id():*{ return this._use_contract_id; }
		
		private var _use_register_id:*;
		public function set use_register_id(use_register_id:*):void{
			this._use_register_id = UseUtil.checkSetData('use_register_id', use_register_id);
			this._dataAliasUpdate['USE_MENU_CONTENT_use_register_id'] = this._dataUpdate['use_register_id'] = use_register_id;
			this._isSave = true;
		}
		public function get use_register_id():*{ return this._use_register_id; }
		
		private var _view_total:*;
		public function set view_total(view_total:*):void{
			this._view_total = UseUtil.checkSetData('view_total', view_total);
			this._dataAliasUpdate['USE_MENU_CONTENT_view_total'] = this._dataUpdate['view_total'] = view_total;
			this._isSave = true;
		}
		public function get view_total():*{ return this._view_total; }
		
		public function get isSave():Boolean{ return this._isSave; }
		public function get useTableAlias():Boolean{ return this._useTableAlias; }
	}
}